package com.web.exiaodao.remote.controller;

import com.web.exiaodao.remote.service.ApiInsHotelRoomsTableService;
import com.web.exiaodao.remote.service.ApiInstitutionService;
import com.web.exiaodao.remote.service.ApiSysAreaService;
import com.web.exiaodao.remote.vo.InsHotelRoomsTableVo;
import com.web.exiaodao.remote.vo.InstitutionVo;
import com.web.exiaodao.remote.vo.SysAreaVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author zhanglin
 * @version V1.0
 * @Title: ApiInstitutionController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 机构表信息接口
 * @date 2016-11-04 10:01:30
 */
@Api(value = "Institution", description = "机构接口", position = 10)
@Controller
@RequestMapping("/api/institution")
public class ApiInstitutionController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ApiInstitutionController.class);

    @Autowired
    private ApiInstitutionService institutionService;

    @Autowired
    private ApiInsHotelRoomsTableService insHotelRoomsTableService;

    @Autowired
    private ApiSysAreaService sysAreaService;

    /**
     * @return
     * @Title: findInstitutionList
     * @Description: 查询所有机构表信息
     */
    @ApiOperation(value = "查询所有机构信息", notes = "查询所有机构信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findInstitutionList", method = RequestMethod.POST)
    public XaResult<List<InstitutionVo>> findInstitutionList(
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("机构区域 (国内0,国外1):institution_Area") @RequestParam(value = "institution_Area", required = false) Long institution_Area,
            @ApiParam("sysAreaId 城市区域id:sysAreaId") @RequestParam(value = "sysAreaId", required = false) Long sysAreaId,
            @ApiParam("机构类型(享老居所0；旅游居所1；养护院2);机构类型(享老居所0；旅游居所1；养护院2):institution_type")
            @RequestParam(value = "institution_type", required = false) Long institution_type
    ) throws BusinessException {
        XaResult<List<InstitutionVo>> xr = new XaResult<List<InstitutionVo>>();
        if ( institution_Area != null && !(institution_Area == 0 || institution_Area == 1) ) {
            xr.error("institution_Area取值错误",true);
            return xr;
        }
        if ( institution_type != null && !(institution_type == 0 || institution_type == 1 || institution_type == 2) ) {
            xr.error("institution_type取值错误",true);
            return xr;
        }
        if (XaUtil.isNotEmpty(institution_Area) && XaUtil.isNotEmpty(sysAreaId)) {
            SysAreaVo sysArea = sysAreaService.findOne(sysAreaId).getObject();
            if (institution_Area != sysArea.getAreaType().intValue()) {
                xr.error("institution_Area和sysAreaId不匹配!");
                return xr;
            }
        }
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
        Map<String , Object> filterParams =  WebUitl.getParametersStartingWith("{}", "search_");
        if (XaUtil.isNotEmpty(institution_Area) && XaUtil.isNotEmpty(institution_type)) {
            filterParams = WebUitl.getParametersStartingWith(
                    "{'search_EQ_institution_Area':'"+  institution_Area +"'" +
                            ",'search_EQ_institution_type':'"+institution_type+"'}", "search_");
        } else if (XaUtil.isNotEmpty(institution_Area)) {
            filterParams   = WebUitl.getParametersStartingWith("{'search_EQ_institution_Area':'" + institution_Area + "'}", "search_");
        } else if (XaUtil.isNotEmpty(institution_type)) {
            filterParams   = WebUitl.getParametersStartingWith("{'search_EQ_institution_type':'" + institution_type + "'}", "search_");
        }
        if (XaUtil.isNotEmpty(sysAreaId)) {
            filterParams.put("EQ_sysAreaId", sysAreaId.toString());
        }
        try {
            return institutionService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
        } catch (Exception e) {
            xr.error(e instanceof  BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage() , new Throwable(e));
        }
        return xr;
    }


    /**
     * @return
     * @Title: findInstitutionById
     * @Description: 根据ID查找单条实体
     * //@param modelId
     */
    @ApiOperation(value = "机构表详情", notes = "查询机构表详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
    @ResponseBody
    @RequestMapping(value = "findInstitutionById", method = RequestMethod.POST)
    public XaResult<InstitutionVo> findInstitutionById(
            @ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id,
            @ApiParam("用户id,字段名:id") @RequestParam(value = "userId", required = false) Long userId
    ) throws BusinessException {
        XaResult<InstitutionVo> xr = new XaResult<InstitutionVo>();
        try {
            return institutionService.findOne(id, userId);
        } catch (Exception e) {
            xr.error(e instanceof  BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage() , new Throwable(e));
        }
        return xr;
    }

    /**
     * @Title: findInsHotelRoomsTableById
     * @Description: 根据ID查找单条实体
     * //@param modelId
     * @return
     */
    @ApiOperation(value="机构的酒店房间表详情",notes="查询机构-酒店房间表详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
    @ResponseBody
    @RequestMapping(value="findInsHotelRoomsTableById",method=RequestMethod.POST)
    public XaResult<InsHotelRoomsTableVo> findInsHotelRoomsTableById(
            @ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id
    ) throws BusinessException{
        XaResult<InsHotelRoomsTableVo> xr = new XaResult<InsHotelRoomsTableVo>();
        try{
            return insHotelRoomsTableService.findOne(id);
        }catch(Exception e){
            xr.error(e instanceof  BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage() , new Throwable(e));
        }
        return xr;
    }
}

